#include "main.h"

#define WIFI_CONFIG_FILE "/reemoon/config/wpa_supplicant.conf"

static lv_obj_t *setting=NULL;
static lv_obj_t *table_list=NULL;
static lv_obj_t *ip_textarea=NULL;
static lv_obj_t *local_ip_textarea=NULL;
static lv_obj_t *port_textarea=NULL;
static lv_obj_t *ssid_textarea=NULL;
static lv_obj_t *password_textarea=NULL;
static lv_obj_t *langugae_drop=NULL;
static lv_obj_t *Interface_drop=NULL;
static lv_obj_t *exit_textarea=NULL;
static void profile_create(lv_obj_t * parent);
static void system_info_fun(lv_obj_t *obj);
static void system_reflash_info(lv_obj_t *table_list);

const char language_list[20]="Chinese\nEnglish";
const char link_mod_list[20]="Wired\nWIFI";
char Interface_name[5*20];
char Interface_address[5][50];
unsigned char Interface_cnt = 0;

static void setting_close(lv_event_t * e)
{
    if (setting == NULL) return ;
    lv_obj_del(setting);
    setting=NULL;
    lv_obj_add_state(StatusBar.time, LV_STATE_FOCUSED);
    return;
}
static void tb_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *obj = lv_event_get_target(e);

    if (code == LV_EVENT_VALUE_CHANGED) {

        int data_size = 1024 * sizeof(char);
        char *data_buff = malloc(data_size);

        // 读取默认的配置信息
        int size = reemoon_read_file(SYSTEM_CONFIG_FILE, data_buff, data_size) ;
        if (size>0)
            parse_sysconf_JSON(data_buff);
        free (data_buff);

        // 同步系统信息页面
        system_reflash_info(table_list);
    }
}

static void Interface_drop_event_cb(lv_event_t * e)
{
    if (e->code == LV_EVENT_VALUE_CHANGED)
    {
        get_system_local_ip_info();
        char interface[20];
        lv_dropdown_get_selected_str(Interface_drop, interface, sizeof(interface));
        int index = lv_dropdown_get_option_index(Interface_drop, interface);
        reemoon_ignore ("index: %d\n", index);
        lv_textarea_set_text(local_ip_textarea, Interface_address[index]);
        if (strstr(interface, "wlan"))
            lv_obj_clear_flag(local_ip_textarea, LV_OBJ_FLAG_CLICKABLE);  // 禁止点击事件
        else
            lv_obj_add_flag(local_ip_textarea, LV_OBJ_FLAG_CLICKABLE);  // 禁止点击事件
    }
}

static void selecte_Interface_event_cb(lv_event_t * e)
{
    get_system_local_ip_info();
}

static void set_local_ip_event_cb(lv_event_t * e)
{
    if (e->code == LV_EVENT_CLICKED)
    {
        get_system_local_ip_info();
        char interface[20];
        lv_dropdown_get_selected_str(Interface_drop, interface, sizeof(interface));
        int index = lv_dropdown_get_option_index(Interface_drop, interface);
        lv_textarea_get_text(local_ip_textarea);
        if (strcmp (lv_textarea_get_text(local_ip_textarea), Interface_address[index]) == 0)
        {
            reemoon_ignore ("Interface_address[%d]: %s\n", index, Interface_address[index]);
            return ;// ip 未修改
        }
        sprintf (Interface_address[index], "%s", lv_textarea_get_text(local_ip_textarea));
        char cmd[100];
        sprintf (cmd, "ifconfig %s %s", interface, lv_textarea_get_text(local_ip_textarea));
        system (cmd);
        reemoon_ignore ("cmd: %s\n", cmd);
    }
}

static void wifi_disconnect_event_cb(lv_event_t *e)
{
    if (e->code == LV_EVENT_CLICKED)
    {
        // reemoon_run_script("./shell/wifi_disconnect.sh &");
        wifi_disconnect();
    }
}

static void wifi_connect_event_cb(lv_event_t * e)
{
    if (e->code == LV_EVENT_CLICKED && ip_textarea != NULL && port_textarea != NULL)
    {
        set_wifi_info(WIFI_CONFIG_FILE, lv_textarea_get_text(ssid_textarea), lv_textarea_get_text(password_textarea));
        // reemoon_run_script("./shell/wifi_connect.sh &");
        wifi_connect();
        // #if RABBITMQ_ENABLE
        // rabbitmq_deinit();
        // rabbitmq_init(); 
        // #endif
    }
}


static void set_ip_event_cb(lv_event_t * e)
{
    if (e->code == LV_EVENT_CLICKED && ssid_textarea != NULL && password_textarea != NULL)
    {
        const char * new_ip = lv_textarea_get_text(ip_textarea);
        const char * new_port = lv_textarea_get_text(port_textarea);
        size_t len = strlen(new_port);
        char filtered_text[100];
        size_t j = 0;
        for (size_t i = 0; i < len; i++) 
        {
            if (new_port[i] >= '0' && new_port[i] <= '9') 
            {
                filtered_text[j++] = new_port[i];
            }
        }
        filtered_text[j] = '\0';
        system_info->server_port = atoi(filtered_text);
        lv_textarea_set_text(port_textarea, filtered_text);
        
        if (new_ip != NULL && strlen(new_ip)>0 && system_info->server_port>0)
            set_server_info(new_ip, system_info->server_port);
        #if RABBITMQ_ENABLE
        rabbitmq_deinit();
        rabbitmq_init(); 
        #endif
    }
}

static void set_language_event_cb(lv_event_t * e)
{
    if (e->code == LV_EVENT_CLICKED)
    {
        char langugae_[10];
        lv_dropdown_get_selected_str(langugae_drop, langugae_, sizeof(langugae_));
        if (strstr(langugae_, "Chinese") && system_info->language == LANGUAGE_CN)
            return;
        if (strstr(langugae_, "English") && system_info->language == LANGUAGE_EN)
            return;
        set_language(langugae_);
        reemoon_system_restart();
    }
}

static void printer_ctrl_event_cb(lv_event_t * e)
{
    if (e->code == LV_EVENT_CLICKED)
    {
        char msg_buffer[256];
        if (strstr(e->user_data, "left"))
        {
            snprintf (&msg_buffer[0],  sizeof(msg_buffer), "{\"%s\":\"%s\",\"exit-num\" :%d}", RABIITMQ_SENDER_ID, CMD_PRINTER_CTRL, reemoon_get_exit_id(1));
            reemoon_msg_send(msg_buffer); 
        }
        else if (strstr(e->user_data, "right") && system_info->exit_cnt == 2)
        {
            snprintf (&msg_buffer[0],  sizeof(msg_buffer), "{\"%s\":\"%s\",\"exit-num\" :%d}", RABIITMQ_SENDER_ID, CMD_PRINTER_CTRL, reemoon_get_exit_id(2));
            reemoon_msg_send(msg_buffer);
        }
    }
}

static void set_exit_cnt_event_cb(lv_event_t * e)
{
    if (e->code == LV_EVENT_CLICKED)
    {
        const char *text = lv_textarea_get_text(exit_textarea);
        size_t len = strlen(text);
        char filtered_text[100];
        size_t j = 0;
        for (size_t i = 0; i < len; i++) 
        {
            if (text[i] >= '0' && text[i] <= '9') 
            {
                filtered_text[j++] = text[i];
            }
        }
        filtered_text[j] = '\0';
        lv_textarea_set_text(exit_textarea, filtered_text);
        if (atoi(filtered_text) != system_info->exit_cnt)
        {
            system_info->exit_cnt = atoi(filtered_text);
            set_exit_cnt(system_info->exit_cnt > 0 ? system_info->exit_cnt : 1) ;
            reemoon_system_restart();
        }
    }
}

static void system_restart_event_cb(lv_event_t * e)
{
    if (e->code == LV_EVENT_CLICKED)
    {
        reemoon_system_restart();
    }
}

static void kb_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * ta = lv_event_get_target(e);
    lv_obj_t * kb = lv_event_get_user_data(e);
    if(code == LV_EVENT_FOCUSED) {
        if(lv_indev_get_type(lv_indev_active()) != LV_INDEV_TYPE_KEYPAD) {
            lv_keyboard_set_textarea(kb, ta);
            lv_obj_set_align(setting, LV_ALIGN_TOP_MID);
            lv_obj_remove_flag(kb, LV_OBJ_FLAG_HIDDEN);
            lv_obj_set_style_max_height(kb, LV_HOR_RES * 2 / 3, 0);
            lv_obj_scroll_to_view_recursive(ta, LV_ANIM_OFF);
            lv_indev_wait_release(lv_event_get_param(e));
        }
    }
    else if(code == LV_EVENT_READY || code == LV_EVENT_CANCEL) {
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
        lv_obj_center(setting);
        lv_indev_reset(NULL, ta);   /*To forget the last clicked object to make it focusable again*/
    }
}

#define SET_COL 70
#define SET_ROW 50
#define FIX 30
static void system_setting(lv_obj_t *obj)
{
    lv_obj_t *parent = obj;
    static lv_coord_t col_dsc[] = { SET_COL, SET_COL, SET_COL, SET_COL, SET_COL, SET_COL, SET_COL, SET_COL, SET_COL, LV_GRID_TEMPLATE_LAST };
    static lv_coord_t row_dsc[] = { FIX, SET_ROW, FIX, FIX, SET_ROW, FIX, SET_ROW, FIX, SET_ROW, LV_GRID_TEMPLATE_LAST };
    lv_obj_set_style_grid_column_dsc_array(parent, col_dsc, 0);
    lv_obj_set_style_grid_row_dsc_array(parent, row_dsc, 0);
    lv_obj_set_layout(parent, LV_LAYOUT_GRID);

    lv_obj_t *cell = NULL;
    lv_obj_t *label = NULL;

    label = lv_label_create(parent);
    lv_obj_add_style(label, &opa_style, 0);
    lv_label_set_text(label, language_text);
    lv_obj_set_grid_cell(label, LV_GRID_ALIGN_SPACE_AROUND, 0, 1,LV_GRID_ALIGN_SPACE_AROUND, 0, 1); 

    langugae_drop = lv_dropdown_create(parent);
    lv_obj_set_size(langugae_drop, SET_COL*3, LV_SIZE_CONTENT);
    lv_obj_set_style_text_font(langugae_drop, LV_FONT_DEFAULT, 0);
    lv_dropdown_set_options(langugae_drop, language_list);
    lv_obj_set_grid_cell(langugae_drop, LV_GRID_ALIGN_SPACE_AROUND, 0, 3,LV_GRID_ALIGN_SPACE_AROUND, 1, 1); 
    if (system_info->language == LANGUAGE_CN)
    {
        lv_dropdown_set_selected(langugae_drop, 0);
    }
    if (system_info->language == LANGUAGE_EN)
    {
        lv_dropdown_set_selected(langugae_drop, 1);
    }

    cell = lv_button_create(parent);
    label = lv_label_create(cell);
    lv_label_set_text(label, APPLY);
    lv_obj_set_grid_cell(cell, LV_GRID_ALIGN_SPACE_AROUND, 3, 1,LV_GRID_ALIGN_SPACE_AROUND, 1, 1); 
    lv_obj_add_event_cb (cell, set_language_event_cb, LV_EVENT_CLICKED, NULL);

    label = lv_label_create(parent);
    lv_obj_add_style(label, &opa_style, 0);
    lv_label_set_text(label, Exit_cnt_text);
    lv_obj_set_grid_cell(label, LV_GRID_ALIGN_SPACE_AROUND, 0, 1,LV_GRID_ALIGN_SPACE_AROUND, 3, 1); 


    exit_textarea = lv_textarea_create(parent);
    char cnt[10];
    sprintf(cnt, "%d", system_info->exit_cnt);
    lv_textarea_set_text(exit_textarea, cnt);
    lv_obj_set_size(exit_textarea, SET_COL*2, LV_SIZE_CONTENT);
    lv_obj_set_grid_cell(exit_textarea, LV_GRID_ALIGN_SPACE_AROUND, 0, 2,LV_GRID_ALIGN_SPACE_AROUND, 4, 1); 
    lv_obj_add_event_cb(exit_textarea, kb_event_cb, LV_EVENT_ALL, rm_kb_num);

    cell = lv_button_create(parent);
    label = lv_label_create(cell);
    lv_label_set_text(label, APPLY);
    lv_obj_set_grid_cell(cell, LV_GRID_ALIGN_SPACE_AROUND, 2, 1,LV_GRID_ALIGN_SPACE_AROUND, 4, 1); 
    lv_obj_add_event_cb (cell, set_exit_cnt_event_cb, LV_EVENT_CLICKED, NULL);


    label = lv_label_create (parent);
    lv_label_set_text (label, Exit_printer_text);
    lv_obj_add_style(label, &opa_style, 0);
    lv_obj_set_grid_cell(label, LV_GRID_ALIGN_SPACE_AROUND, 0, 2,LV_GRID_ALIGN_SPACE_AROUND, 6, 1); 

    cell = lv_button_create(parent);
    lv_obj_set_size(cell, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
    label=lv_label_create (cell);
    lv_label_set_text (label, print_left_text);
    lv_obj_set_grid_cell(cell, LV_GRID_ALIGN_SPACE_AROUND, 0, 1,LV_GRID_ALIGN_SPACE_AROUND, 7, 1); 
    lv_obj_add_event_cb (cell, printer_ctrl_event_cb, LV_EVENT_CLICKED, "left");
    
    if (system_info->exit_cnt == 2)
    {
        cell = lv_button_create(parent);
        lv_obj_set_size(cell, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
        label=lv_label_create (cell);
        lv_label_set_text (label, print_right_text);
        lv_obj_set_grid_cell(cell, LV_GRID_ALIGN_SPACE_AROUND, 3, 1,LV_GRID_ALIGN_SPACE_AROUND, 7, 1); 
        lv_obj_add_event_cb (cell, printer_ctrl_event_cb, LV_EVENT_CLICKED, "right");
    }

    cell = lv_button_create(parent);
    label = lv_label_create(cell);
    lv_label_set_text(label, SYSSTART);
    lv_obj_add_flag(cell, LV_OBJ_FLAG_IGNORE_LAYOUT);
    lv_obj_set_align(cell, LV_ALIGN_BOTTOM_RIGHT);
    // lv_obj_set_grid_cell(cell, LV_GRID_ALIGN_SPACE_AROUND, 8, 1,LV_GRID_ALIGN_SPACE_AROUND, 7, 1); 
    lv_obj_add_event_cb (cell, system_restart_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_t *close = lv_button_create (parent);
    label = lv_label_create (close);
    lv_label_set_text(label, BACK);
    lv_obj_center(label);
    lv_obj_add_flag(close, LV_OBJ_FLAG_IGNORE_LAYOUT);
    lv_obj_set_align(close, LV_ALIGN_BOTTOM_LEFT);
    lv_obj_add_event_cb(close, setting_close, LV_EVENT_CLICKED, setting);
}


static void network_setting(lv_obj_t *obj)
{
    lv_obj_t *parent = obj;
    static lv_coord_t col_dsc[] = { SET_COL, SET_COL, SET_COL, SET_COL, SET_COL, SET_COL, SET_COL, SET_COL, SET_COL, SET_COL, SET_COL, SET_COL, LV_GRID_TEMPLATE_LAST };
    static lv_coord_t row_dsc[] = { SET_ROW, SET_ROW, SET_ROW, SET_ROW, SET_ROW, SET_ROW, SET_ROW, SET_ROW, LV_GRID_TEMPLATE_LAST };
    lv_obj_set_style_grid_column_dsc_array(parent, col_dsc, 0);
    lv_obj_set_style_grid_row_dsc_array(parent, row_dsc, 0);
    lv_obj_set_layout(parent, LV_LAYOUT_GRID);

    lv_obj_t *cell = NULL;
    lv_obj_t *label = NULL;

    label = lv_label_create(parent);
    lv_obj_add_style(label, &opa_style, 0);
    lv_label_set_text(label, "设置本地网络");
    lv_obj_set_grid_cell(label, LV_GRID_ALIGN_SPACE_AROUND, 0, 3,LV_GRID_ALIGN_SPACE_AROUND, 0, 1); 

    Interface_drop = lv_dropdown_create(parent);
    lv_obj_set_size(Interface_drop, SET_COL*3, LV_SIZE_CONTENT);
    lv_obj_set_style_text_font(Interface_drop, LV_FONT_DEFAULT, 0);
    get_system_local_ip_info();
    lv_dropdown_set_options(Interface_drop, (char *)Interface_name);
    lv_dropdown_set_selected(Interface_drop, 0);
    lv_obj_add_event_cb(Interface_drop,Interface_drop_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
    lv_obj_set_grid_cell(Interface_drop, LV_GRID_ALIGN_SPACE_AROUND, 0, 3,LV_GRID_ALIGN_SPACE_AROUND, 1, 1);

    local_ip_textarea = lv_textarea_create(parent);
    lv_obj_set_size(local_ip_textarea, SET_COL*5, LV_SIZE_CONTENT);
    lv_textarea_set_text(local_ip_textarea, Interface_address[0]);
    // lv_textarea_set_placeholder_text(local_ip_textarea, Interface_address[0]);
    lv_obj_add_event_cb(local_ip_textarea, kb_event_cb, LV_EVENT_ALL, rm_kb);
    lv_obj_set_grid_cell(local_ip_textarea, LV_GRID_ALIGN_SPACE_AROUND, 4, 1,LV_GRID_ALIGN_SPACE_AROUND, 1, 1);

    cell = lv_button_create(parent);
    label = lv_label_create(cell);
    lv_label_set_text(label, APPLY);
    lv_obj_add_event_cb (cell, set_local_ip_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_set_grid_cell(cell, LV_GRID_ALIGN_SPACE_AROUND, 8, 1,LV_GRID_ALIGN_SPACE_AROUND, 1, 1); 

    label = lv_label_create(parent);
    lv_obj_add_style(label, &opa_style, 0);
    lv_obj_set_size(label, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
    lv_label_set_text(label, "WIFI "LV_SYMBOL_WIFI);
    lv_obj_set_style_text_font(label, LV_FONT_DEFAULT, 0);
    lv_obj_set_grid_cell(label, LV_GRID_ALIGN_SPACE_AROUND, 0, 3,LV_GRID_ALIGN_SPACE_AROUND, 2, 1); 

    ssid_textarea = lv_textarea_create(parent);
    lv_obj_set_size(ssid_textarea, SET_COL*5, LV_SIZE_CONTENT);
    lv_obj_add_event_cb(ssid_textarea, kb_event_cb, LV_EVENT_ALL, rm_kb);
    lv_obj_set_grid_cell(ssid_textarea, LV_GRID_ALIGN_SPACE_AROUND, 0, 3,LV_GRID_ALIGN_SPACE_AROUND, 3, 1);

    password_textarea = lv_textarea_create(parent);
    lv_obj_set_size(password_textarea, SET_COL*5, LV_SIZE_CONTENT);
    lv_obj_add_event_cb(password_textarea, kb_event_cb, LV_EVENT_ALL, rm_kb);
    lv_obj_set_grid_cell(password_textarea, LV_GRID_ALIGN_SPACE_AROUND, 4, 3,LV_GRID_ALIGN_SPACE_AROUND, 3, 1);

    char ssid[100];
    char password[100];
    get_wifi_info(WIFI_CONFIG_FILE, ssid, password) ;
    lv_textarea_set_text(ssid_textarea, ssid);
    lv_textarea_set_text(password_textarea, password);

    cell = lv_button_create(parent);
    label = lv_label_create(cell);
    lv_label_set_text(label, CONNECT);
    lv_obj_add_event_cb (cell, wifi_connect_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_set_grid_cell(cell, LV_GRID_ALIGN_SPACE_AROUND, 8, 1,LV_GRID_ALIGN_SPACE_AROUND, 3, 1); 

    cell = lv_button_create(parent);
    label = lv_label_create(cell);
    lv_label_set_text(label, DISCONNECT);
    lv_obj_add_event_cb (cell, wifi_disconnect_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_set_grid_cell(cell, LV_GRID_ALIGN_SPACE_AROUND, 10, 2,LV_GRID_ALIGN_SPACE_AROUND, 3, 1); 

    label = lv_label_create(parent);
    lv_obj_add_style(label, &opa_style, 0);
    lv_label_set_text(label, "设置服务器ip和端口");
    lv_obj_set_grid_cell(label, LV_GRID_ALIGN_SPACE_AROUND, 0, 3,LV_GRID_ALIGN_SPACE_AROUND, 4, 1); 

    ip_textarea = lv_textarea_create(parent);
    lv_obj_set_size(ip_textarea, SET_COL*5, LV_SIZE_CONTENT);
    lv_obj_add_event_cb(ip_textarea, kb_event_cb, LV_EVENT_ALL, rm_kb);
    lv_obj_set_grid_cell(ip_textarea, LV_GRID_ALIGN_SPACE_AROUND, 0, 3,LV_GRID_ALIGN_SPACE_AROUND, 5, 1);

    port_textarea = lv_textarea_create(parent);
    lv_obj_set_size(port_textarea, SET_COL*5, LV_SIZE_CONTENT);
    lv_obj_add_event_cb(port_textarea, kb_event_cb, LV_EVENT_ALL, rm_kb);
    lv_obj_set_grid_cell(port_textarea, LV_GRID_ALIGN_SPACE_AROUND, 4, 3,LV_GRID_ALIGN_SPACE_AROUND, 5, 1);
    
    char port[10];
    sprintf(port, "%d", system_info->server_port);
    lv_textarea_set_text(ip_textarea, system_info->server_ip);
    lv_textarea_set_text(port_textarea, port);

    cell = lv_button_create(parent);
    label = lv_label_create(cell);
    lv_label_set_text(label, APPLY);
    lv_obj_add_event_cb (cell, set_ip_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_set_grid_cell(cell, LV_GRID_ALIGN_SPACE_AROUND, 8, 1,LV_GRID_ALIGN_SPACE_AROUND, 5, 1); 

    // 返回按键
    cell = lv_button_create (parent);
    label = lv_label_create (cell);
    lv_label_set_text(label, BACK);
    lv_obj_center(label);
    lv_obj_add_flag(cell, LV_OBJ_FLAG_IGNORE_LAYOUT);
    lv_obj_set_align(cell, LV_ALIGN_BOTTOM_LEFT);
    lv_obj_add_event_cb(cell, setting_close, LV_EVENT_CLICKED, setting);
}

static void system_reflash_info(lv_obj_t *table_list)
{
    lv_table_set_cell_value(table_list, 0, 0, "server ip:");
    lv_table_set_cell_value(table_list, 0, 1, strlen(system_info->server_ip) > 0 ? system_info->server_ip : "");
    lv_table_set_cell_value(table_list, 1, 0, "server port:");
    lv_table_set_cell_value_fmt(table_list, 1, 1, "%d", system_info->server_port);

    lv_table_set_cell_value(table_list, 2, 0, "exit count:");
    lv_table_set_cell_value_fmt(table_list, 2, 1, "%d", system_info->exit_cnt);

    lv_table_set_cell_value(table_list, 3, 0, "font:");
    if (system_info->font == FT_SMALL)
        lv_table_set_cell_value(table_list, 3, 1, "small");
    else if (system_info->font == FT_MEDIUM)
        lv_table_set_cell_value(table_list, 3, 1, "medium");
    else if (system_info->font == FT_LARGE)
        lv_table_set_cell_value(table_list, 3, 1, "large");

    lv_table_set_cell_value(table_list, 4, 0, "language:");
    if (system_info->language == LANGUAGE_CN)
        lv_table_set_cell_value(table_list, 4, 1, "Chinese");
    else
        lv_table_set_cell_value(table_list, 4, 1, "English");

    lv_table_set_cell_value(table_list, 5, 0, "version:");
    lv_table_set_cell_value(table_list, 5, 1, strlen(system_info->version) > 0 ? system_info->version : "");
}

static void system_info_fun(lv_obj_t *obj)
{
    lv_obj_t *parent = obj;

    table_list = lv_table_create(parent);
    // 设置表格的位置，这里将表格放置在父对象的左上角
    // lv_obj_center(table_list);
    lv_obj_set_align(table_list, LV_ALIGN_TOP_MID);
    // 设置表格的大小，宽度为200像素，高度为150像素
    lv_obj_set_size(table_list, LV_SIZE_CONTENT, LV_SIZE_CONTENT);

    // 设置表格的行数和列数，这里设置为3行2列
    lv_table_set_row_cnt(table_list, 6);
    lv_table_set_col_cnt(table_list, 2);

    // 设置表格列的宽度，这里将第一列宽度设置为100像素
    lv_table_set_col_width(table_list, 0, 250);
    lv_table_set_col_width(table_list, 1, 600);
    // 向表格中添加数据
    system_reflash_info(table_list);
    // 创建一个样式对象，用于设置表头样式
    static lv_style_t style_header;
    lv_style_init(&style_header);
    // 设置字体为加粗
    lv_style_set_text_font(&style_header, ft_medium);
    // 将样式应用到表头单元格类型为1的单元格
    lv_obj_add_style(table_list, &style_header, LV_STATE_FOCUSED | LV_STATE_CHECKED);
    lv_obj_t *close = lv_button_create (parent);
    lv_obj_t * label = lv_label_create (close);
    lv_label_set_text(label, BACK);
    lv_obj_center(label);
    lv_obj_add_flag(close, LV_OBJ_FLAG_IGNORE_LAYOUT);
    lv_obj_set_align(close, LV_ALIGN_BOTTOM_LEFT);
    lv_obj_add_event_cb(close, setting_close, LV_EVENT_CLICKED, setting);
}

void setting_show(lv_event_t * e)
{
    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_text_font(&style, ft_medium); // 字体
    if (setting != NULL) return ;
    setting = lv_obj_create (lv_layer_top());
    lv_obj_set_size (setting, SCREEN_INFO.DISPLAY_W/5*3, SCREEN_INFO.DISPLAY_H/5*4);
    lv_obj_center(setting);
    // lv_obj_set_align(setting, LV_ALIGN_TOP_MID);

    int tab_h = 70;
    lv_obj_t * tv = lv_tabview_create(setting);
    lv_obj_add_style(tv, &style, 0);
    lv_tabview_set_tab_bar_size(tv, tab_h);
    lv_obj_t * tab_bar = lv_tabview_get_tab_bar(tv);
    lv_obj_set_style_pad_left(tab_bar, LV_HOR_RES / 5*1, 0);
    lv_obj_add_event_cb(tv, tb_event_cb, LV_EVENT_VALUE_CHANGED, NULL);

    lv_obj_t * tx=NULL;
    tx = lv_tabview_add_tab(tv, SYSSETTING);
    system_setting(tx);
    tx = lv_tabview_add_tab(tv, NETWORKSETTING);
    network_setting(tx);
    tx = lv_tabview_add_tab(tv, SYSINFO);
    system_info_fun(tx);
}
